<?php

namespace app\home\controller;


use app\admin\model\Book;
use app\admin\model\Notice;
use app\home\model\Active;
use app\home\model\Area;
use app\home\model\Content;
use app\home\model\Stoptell;
use app\home\model\Visiting;
use app\home\model\Yactive;
use think\Controller;
use think\Db;
use think\Request;
class User extends Controller
{
    //獲取是時間
    public function __construct(){
        parent::__construct();
        $atime = strtotime(date("Y-m-d 16:00")); //当日16:00
        (time()>$atime)?$a=2:$a=1; //超过当日16:00 延后一天
        for ($i=$a;$i<=$a+6;$i++){ //预约7天内的时间
            $time[]= date("Y-m-d",strtotime(date('Y-m-d')."+$i day"));
        }
        $yuyue = new Visiting();
        $yuyue = $yuyue->selectColumn($time[0],"A",0);
        $this->assign('yuyue',$yuyue);//时间段
        $this->assign('visit_time',config('visit_time'));    //4个时间段
        $this->assign('visit_time2',config('visit_time2'));  //7个时间段
        $this->assign("position_type",config("position_type"));
        $this->assign('time',$time);//时间段
    }


    //查询  预约记录
    public function login(){
        $model = new Visiting();  //实例化  预约表
        $modely=new Yactive();
        $data = input("post.");
        if($data['code'] == null ){
            return 1;
        }
        if(cache("register_code") == false){
            return 2;
        }
        if(cache("register_code")!= $data['code']){
            return 3;
        }
        session('mobile',$data['mobile']);
        $res=$model->where("mobile",$data['mobile'])->find();
        $active = $modely->where("mobile",$data['mobile'])->find();
        if($res == null  && $active== null){
             return 4;
        }elseif($active){
             return $active;
        }elseif ($res){
            return $res;
        }else{
            return 4;
        }
    }

    /*
     *  查询获取预约信息 发送验证码
     * */
    public function regsend(){
        $data=input("post.");
        $code =  str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        cache("register_code",$code,60);
        $result = sendSMS("【五四宪法馆】您好,您的查询验证码为：".$code,$code,$data['mobile']);
        if($result){
            return 1;
        }
    }

    //首页
    public function index(){
        if($this->isMobile()){
            echo "<script>window.location.href = '/wap'</script>";exit();
        }
        $model=new Content();
        $list=$model->where("status",1)->field("content")->order("id desc")->select();  //获取公告信息
        $this->assign("list",$list);
        $model=new Active();
        $result=$model->where("switch",1)->select();
        foreach($result as $k =>$val) {
            $result[$k]['end']   = strtotime($val['endday']) >= time() ? 0 : 1;
            $result[$k]['enday'] = ceil(round((strtotime($val['endday']) - time()) / 86400, 1));
            $result[$k]['timer'] = config("visit_time2")[$val['timer']];
        }
        //dd(objToArray($result));
        $this->assign("active",$result);
        $result = Notice::where("id",1)->find();
        $this->assign("result",$result);
        return $this -> fetch();
    }

    public function isMobile()
    {
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
            return true;

        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset ($_SERVER['HTTP_VIA']))
        {
            // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }
        // 脑残法，判断手机发送的客户端标志,兼容性有待提高
        if (isset ($_SERVER['HTTP_USER_AGENT']))
        {
            $clientkeywords = array ('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile');
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
                return true;
        }
        // 协议法，因为有可能不准确，放到最后判断
        if (isset ($_SERVER['HTTP_ACCEPT']))
        {
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
            {
                return true;
            }
        }
        return false;
    }

    //清楚session
    public function tuichu(){
          \think\Session::delete("mobile");
          return $this->redirect("home/User/index");
    }


    //用户预约成功回执
    public function order_success(){
        $id = $this->request->param('id');
        $visit_info = Db::table('visiting')->find($id);
        $this->assign('visit_info',$visit_info);
        return $this -> fetch();
    }

    //我的预约
    public function my_order(){
        $data = input("get.");
        $order_list = new Visiting();
        $this->assign('visit_list',$order_list->mobileSelect($data['mobile']));
        return $this -> fetch();
    }

    //预约过程
    public function information(){
        if(request()->isPost()){
            $data = array_filter(input("post."));
            $cache_key = 'yy_'.$data['mobile'];
            if(cache($cache_key)){
                $this->error('请勿频繁提交');
//                echo "<script> alert('请勿频繁提交');history.go(-1);</script>";exit;
            }
            if(isset($data['views']) > 100){
                $this->error('添加人数不能超过100');

//                echo "<script> alert('添加人数不能超过100');history.go(-1);</script>";exit;
            }
            if(cache("yy_code") == false){
                $this->error('验证码无效，请重新获取');

//                echo "<script> alert('请输入正确的验证码');history.go(-1);</script>";exit;
            }
            if(cache("yy_code")!= $data['code']){
                $this->error('预约失败，验证码输入错误');

//                echo "<script> alert('预约失败，验证码输入错误');history.go(-1);</script>";exit;
            }
            $stopyuyue = Stoptell::where("mobile",$data['mobile'])->find();
            if($stopyuyue){
                echo "<script>history.go(-1);</script>";exit;
            }
            //$data['explain']=$data['explain']??0;  //个人预约 默认设置0
            if(isset($data['explain'])){
                switch ($data['venue']){
                    case "A":
                        $data['dinner']=$data['arrivea_part'] == '上午'?0:1;
                        $data['arrival_time']=$data['arrivea'];
                        break;
                    case "B":
                        $data['dinner']=$data['arriveb_part'] == '上午'?0:1;
                        $data['arrival_time']=$data['arriveb'];
                        break;
                }
            }
            if(!isset($data['between_time'])){
                $data['between_time']=0;
            }
            if(!isset($data['time1'])){
                $data['time1']=0;
            }
            //dd($data);
            Session('mobile',$data['mobile']);
            $data['code'] =   str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
            $add = new Visiting();
            $time = config("visit_time2");
            if($data['venue']=="C"){
                if(isset($data['explain'])){
                    $data['dinner']=$data['arrivea_part'] == '上午'?0:1;
                    $data['arrival_time']=$data['arrivea'];
                }

                $data['venue'] = 'A';
                if(!isset($data['explain'])){
                    foreach($time as $key =>$value){
                        if($data['time'] == $value){
                            $data['between_time']=$key;
                        }
                    }
                }

                if(isset($data['explain'])){
                    $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
                    if(in_array($data['dinner'],$stop)){
                        $this->error('预约时间已满，请选择其他时间');

//                        echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
                    }
                }

                if(isset($data["arriveb_part"])){
                    $data['dinner'] = $data['arriveb_part']=='上午'?0:1;
                    $data['arrival_time'] = $data['arriveb'];
                }

                if(isset($data['explain'])){
                    $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
                    if(in_array($data['dinner'],$stop)){
                        $this->error('预约时间已满，请选择其他时间');

//                        echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
                    }
                }



                /*$view = Visiting::where(["venue"=>$data['venue'],'day'=>$data['day'],'between_time'=>$data['between_time'],'explain'=>1,'status'=>1])->sum('views');
                if(isset($data['views'])){
                    if($view +  $data['views'] >= 100){
                        echo "<script> alert('当前时间".$data['day'].$data['time']."预约人数已经大于100人，当前人数".$view."');history.go(-1);</script>";exit;
                    }
                }*/

                if(isset($data['explain'])){
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['arrival_time']."     北山街馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：北山街84号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }else{
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['time']."     北山街馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：北山街84号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }
                $result = $add->addeditVisit($data);
                $data['venue'] = 'B';
                if(isset($data['explain'])){
                    $data['dinner']=$data['arriveb_part'] == '上午'?0:1;
                    $data['arrival_time']=$data['arriveb'];
                }
                if(isset($data['explain'])){
                    $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
                    if(in_array($data['dinner'],$stop)){
                        $this->error('预约时间已满，请选择其他时间');

//                        echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
                    }
                }
                foreach ($time as $key => $value) {
                    if ($data['time1'] == $value) {
                        $data['between_time'] = $key;
                    }
                }
                $data['day']=$data['day1'];
                /*$view = Visiting::where(["venue"=>$data['venue'],'day'=>$data['day'],'between_time'=>$data['between_time'],'explain'=>1,'status'=>1])->sum('views');
                if(isset($data['views'])){
                    if($view +  $data['views'] >= 100){
                        echo "<script> alert('当前时间".$data['day'].$data['time']."预约人数已经大于100人，当前人数".$view."');history.go(-1);</script>";exit;
                    }
                }*/

                $data['code'] =   str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);

                if(isset($data['explain'])){
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['arrival_time']."     栖霞岭馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：栖霞岭54号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }else{
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['time']."     栖霞岭馆区，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。地址：栖霞岭54号。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }

                $result1 = $add->addeditVisit($data);
            }else{
                if(!isset($data['explain'])){
                    foreach($time as $key =>$value){
                        if($data['time'] == $value){
                            $data['between_time']=$key;
                        }
                    }
                }

                if(isset($data["arrivea_part"])||isset($data["arriveb_part"])){
                    if(isset($data['arrivea_part'])) {
                        $data['dinner'] = $data['arrivea_part']=='上午'?0:1;
                    }else{
                        $data['dinner'] = $data['arriveb_part']=='上午'?0:1;
                    }
                    if(isset($data['arrivea'])){
                        $data['arrival_time'] = $data['arrivea'];
                    }else{
                        $data['arrival_time'] = $data['arriveb'];
                    }
                }
                if(isset($data['explain'])){
                    $stop = objToArray($add->selectColumn($data['day'],$data['venue'],$data['explain']));
                    if(in_array($data['dinner'],$stop)){
                        $this->error('预约时间已满，请选择其他时间');

//                        echo "<script> alert('预约时间已满，请选择其他时间');history.go(-1);</script>";exit;
                    }
                }

                /*$view = Visiting::where(["venue"=>$data['venue'],'day'=>$data['day'],'between_time'=>$data['between_time'],'explain'=>1,'status'=>1])->sum('views');
                if(isset($data['views'])){
                    if($view +  $data['views'] >= 100){
                        echo "<script> alert('当前时间".$data['day'].$data['time']."预约人数已经大于100人，当前人数".$view."');history.go(-1);</script>";exit;
                    }
                }*/
                $guan = $data['venue']=='A'?'北山街馆区，地址是北山街84号':'栖霞岭馆区，地址是栖霞岭54号';

                if(isset($data['explain'])){
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['arrival_time'].$guan."  ，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }else{
                    sendSMS("【五四宪法馆】预约成功。预约码：".$data['code']."。您已预约".$data['day'] ."    ".$data['time'] .$guan."   ，请您携带有效身份证件按时签到参观。团队参观请携带介绍信。取消或改期截止时间为参观前一日14点前。点击".\request()->domain()."查看您的预约信息",$data['code'],$data['mobile']);
                }
                $result = $add->addeditVisit($data);
            }

            cache($cache_key,1,5);
            cache("yy_code",0);
            $this->success('预约成功','/my_order?mobile='. session('mobile'));

            echo "<script> alert('预约成功');location.href = '/my_order?mobile=". session('mobile')."';</script>";
        }
        $result = new  \app\home\model\User();
        $this->assign("list" ,$list = $result->findAll(session("mobile")));
        $model=new Area();
        $province =$model->where("pid",0)->column("id,name");   //获取省数据
        $this->assign("province",$province);
        $city = Area::where("pid",933)->column("id,name");
        $this->assign("city",$city);
        return $this -> fetch();
    }

    //发送预约验证码
    public function sendSNS(){
        $data = input("post.");
        $code =  str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        cache('yy_code',$code,60);
        $stopyuyue = Stoptell::where("mobile",$data['mobile'])->find();
        if($stopyuyue){
            echo "<script>history.go(-1);</script>";exit;
        }
        $result = sendSMS("【五四宪法馆】您好,您的预约验证码为：".$code,$code,$data['mobile']);
        if($result){
            return 1;
        }
    }

    /**
     * 市查询
     */
    public function province(){
        $model=new Area();
        $data = input("get.");
        $data = $model->where("pid",$data['id'])->column("id,name");
        return $data;
    }

    /**
     * 区查询
     */
    public function city(){
        $model=new Area();
        $data = input("get.");
        $data = $model->where("pid",$data['id'])->column("id,name");
        return $data;
    }

    /*
     * 预约
     * */
    /*public function runimformation(){
        $data1 = input("post.");
        $yuyue = new Visiting();
        $visit_time = config('visit_time2') ;
        $visit_time2= config('visit_time')  ;
        $data=array();
        if($data1['venue']=='C'){
            $yuyuea = objToArray($yuyue->selectColumn($data1['day'],'A',$data1['explain']));
            $yuyueb = objToArray($yuyue->selectColumn($data1['day1'],'B',$data1['explain']));
            $data['yuyue']=array_values($yuyuea);
            $data['yuyueb']=array_values($yuyueb);
        }else{
            unset($data['day1']);
            $yuyue = objToArray($yuyue->selectColumn($data1['day'],$data1['venue'],$data1['explain']));
            $data['yuyue']=array_values($yuyue);
        }
//
        $data['yuyuea'] = array();
        $data['yuyuebb']= array();
        if($data1['explain'] ==1 ){
            $data['visit']=array_values($visit_time2);
            foreach($visit_time as $k=>$v){
                foreach($data['yuyue'] as $key=>$value){
                    if($k == $value){
                        array_push($data['yuyuea'],$v);
                    }
                }
            }
            if(isset($data['yuyueb'])){
                foreach($visit_time as $k=>$v){
                    foreach($data['yuyueb'] as $key=>$value){
                        if($k == $value){
                            array_push($data['yuyuebb'],$v);
                        }
                    }
                }
            }
        }else{
            $data['visit']=array_values($visit_time);
        }
        $data["explain"]  = $data1['explain'] ;
//        dd($data);
        return $data;
    }*/

    public function runimformation(){
        $data1 = input("post.");
        $yuyue = new Visiting();
        $visit_time = config('visit_time2') ;
        $visit_time2= config('visit_time')  ;
        $data=array();
        if($data1['venue']=='C'){
            $yuyuea = objToArray($yuyue->selectColumn($data1['day'],'A',$data1['explain']));
            $yuyueb = objToArray($yuyue->selectColumn($data1['day1'],'B',$data1['explain']));
            $data['venue'] = '北山街馆区';
            $data['venue1'] = '栖霞岭馆区';
            $data['yuyue']=array_values($yuyuea);
            $data['yuyueb']=array_values($yuyueb);
        }else{
            unset($data['day1']);
            $yuyue = objToArray($yuyue->selectColumn($data1['day'],$data1['venue'],$data1['explain']));
            $data['yuyue']=array_values($yuyue);
            $data['venue'] = $data1['venue']=='A'?'北山街馆区':'栖霞岭馆区';
        }
        $data['yuyuea'] = array();
        $data['yuyuebb']= array();
        if($data1['explain'] ==1 ){
            $data['yuyue'] = implode(",",$data['yuyue']);
            if(isset($data['yuyueb'])){
                $data['yuyueb'] = implode(",",$data['yuyueb']);
            }
        }else{
            $data['visit']=array_values($visit_time);
        }
        $data["explain"]  = $data1['explain'] ;
//        dd($data);
        return $data;
    }




    //取消预约
    public function delYuyue(){
        $data=input("post.");
        $del = new Visiting();
        $result = $del->delYuyue($data);
         return info($result['msg'],$result['code'],$result['url']);
    }

    //修改预约
    public function edit(){
            if(request()->isPost()){
                $data = input("post.");
                /*if($data['cache']!=1){
                    if($data['code']!=cache($data['cache'])){
                        echo "<script> alert('添加失败，验证码输入错误');history.go(-1);</script>";exit;
                    }else if(cache($data['cache'] )== " "){
                        echo "<script> alert('验证码已过期,请重新发送');history.go(-1);</script>";exit;
                    }
                }*/
                //dd($data);
                if(!isset($data['time'])){
                    $data['time']=0;
                }
                if(!isset($data['between_time'])){
                    $data['between_time']=0;
                }
                if(!isset($data['dinner'])){
                    $data['dinner']=Null;
                }else{
                    $data['dinner']= $data['dinner']=='上午'?0:1;
                }
                Session('mobile',$data['mobile']);

                $add = new Visiting();
                $time = config("visit_time2");
                foreach($time as $key =>$value){
                    if($data['time'] == $value){
                        $data['between_time']=$key;
                    }
                }
                if($data['explain']==1){
                    $view = Visiting::where(["venue"=>$data['venue'],'day'=>$data['day'],'between_time'=>$data['between_time'],'explain'=>1,'status'=>1])->sum('views');
                    if($view  >=100){
                        echo "<script> alert('当前时间".$data['day'].$data['time']."预约人数已经大于100人，当前人数".$view."');history.go(-1);</script>";exit;
                    }
                }

                $result = $add->editVisit($data);
                $this->success('改签成功','/my_order?mobile='. session("mobile"),10);
//                echo "<script> alert('修改成功'); window.location.href = '/my_order?mobile=". session("mobile")."';</script>";
            }
            $data = input("get.");
            $yuyue = new Visiting();
            $yulist = $yuyue->selectColumn($data['day'],$data['venue'],$data['explain']);
            $data['venue']=($data['venue'] == 'A')?1:2;
            $data['between_time'] =  $yuyue->SelectAll($data['id'])['between_time'];
            if($data['explain'] == 0){
                $visit_time=config("visit_time2");
            }else{
                $visit_time=config("visit_time");
                $time = config("visit_time2");
                foreach($time as $key =>$value){
                    if($data['between_time'] == $key){
                        $data['between_time'] =$value;
                    }
                }
                foreach($visit_time as $k=>$value){
                    if($data['between_time'] == $value){
                        $data['between_time'] =$k;
                    }
                }
            }
            $list = $yuyue->SelectAll($data['id']);

            $my_date = explode(':',$list['arrival_time']);    //时间分割
            $arriveTime = $my_date[0] ?? '';  //获取 小时
            $minute = $my_date[1] ?? '';    //获取分钟
            $am_select = $pm_select = [0,0];
            if($list['dinner'] == 0){
                //am
                $am_select = [$arriveTime,$minute];
            }else{
                //pm
                $pm_select = [$arriveTime,$minute];
            }
            $this->assign(["yuyue"=>$yulist, "list"=>$list, "data"=>$data,"visit_time"=>$visit_time,'am_select'=>$am_select , 'pm_select'=>$pm_select]);
            return view();
    }

    /*
     * 评价
     * */
    public function evaluate(){
        $model = new Visiting();  //实例化  表

        $id=input('id');
        $data = $model->find($id);
        if(!$data){
            $this->error('非法操作');
        }
        if($data['status']==4){
            $this->error('以做出评价，请勿重复提交');
        }
        if(Request::instance()->isPost()){
            $data=input('post.');
            $data['status']=2;   //设置 以评论状态
            $res=$model->where('id',$data['id'])->update(["evcontent"=>$data['evcontent'],"evaluate"=>$data['evaluate'],"status"=>$data['status']]);
            if($res){
                $this->success('评价已提交，感谢您的支持','/my_order?mobile='. session("mobile"),10);
//                echo "<script> alert('评价成功');window.location.href = 'my_order?mobile=". session("mobile")."';</script>";
            }
        }else{

            $id=input('get.id');
            $this->assign("id",$id);
            return view();
        }
    }

    /*
     * 我的活动记录
     * */
    public function activity_log(){
        $data=input('get.');
        $model=new Yactive();
        $res=$model->where("switch", 1)->where("mobile", $data['mobile'])->order("id desc")->with("active")->select();
        foreach($res as $key=>$value){
            $res[$key]['active']['timer']=config('visit_time2')[$value['active']['timer']];
        }
        $this->assign("list",$res);
        return view();
    }

    /*
     * 查看活动详情
     * */
    public function active_xq(){
        $data = input("get.");
        $model=new Yactive();
        $res=$model->where("id", $data['id'])->with('active1')->find();
        $res['del'] = strtotime($res['day'])-86400 <= time() ?1 :0;
        $this->assign("active_xq",$res);
        return view();
    }

    /*
     * 取消活动
     * */
    public function active_del(){
        $data=input('post.');
        $model=new Yactive();
        $res=$model->where("id", $data['id'])->update(["status"=>0]);
        if($res){
            return 1;   // 成功
        }
    }

    /*
     * 活动列表
     * */
    public function activity_list(){
        $model=new Active();
        $result=$model->where("switch",1)->order("id desc")->select();
        foreach($result as $k =>$val) {
            $result[$k]['end']   = strtotime($val['endday']) >= time() ? 0 : 1;
            $result[$k]['enday'] = ceil(round((strtotime($val['endday']) - time()) / 86400, 1));
            $result[$k]['timer'] = config("visit_time2")[$val['timer']];
        }
        //dd(objToArray($result));
        $this->assign("active",$result);
        return view();
    }

    /*
     * 活动预约
     * */
    public function activity_deta(){
        $model=new Active();
        $model1=new Yactive();
        if(Request::instance()->isPost()){
            $data   = array_filter(input("post."));
            $cache_key = 'yy_'.$data['mobile'];
            if(cache($cache_key)){
                echo "<script> alert('请勿频繁提交');history.go(-1);</script>";exit;
            }
            if(cache("yy_code") == false){
                echo "<script> alert('请输入正确的验证码');history.go(-1);</script>";exit;
            }
            if(cache("yy_code")!= $data['code']){
                echo "<script> alert('预约失败，验证码输入错误');history.go(-1);</script>";exit;
            }
            $stopyuyue = Stoptell::where("mobile",$data['mobile'])->find();
            if($stopyuyue){
                echo "<script>history.go(-1);</script>";exit;
            }
            $active = $model->where("switch", 1)->where('id', $data['id'])->find();
            if($data['views'] > $active['views']){
                echo "<script> alert('添加人数不能超过{$active['views']}');history.go(-1);</script>";exit;
            }
            $sum   = $model1->where("active_id", $data['id'])->where("status",1)->where("day", $data['day'])->sum("views");
            if($sum){
                if ($data['views'] + $sum > $active['views']) {
                    echo "<script> alert('当前时段已人数超出,请预约其他时间');history.go(-1);</script>";exit;
                }
            }
            unset($data['code']);
            $data['active_id']=$data['id'];
            unset($data['id']);
            session("mobile",$data['mobile']);
            $data['status'] = 1;
            $code =  str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
            sendSMS("【五四宪法馆】预约成功。活动预约码：".$code."。",$code,$data['mobile']);
            $data['code']=$code;
            $model1->create($data);
            cache($cache_key,1,5);
            echo "<script> alert('预约成功');window.location.href = '/activity_log?mobile=". $data['mobile']."';</script>";
        }else{
            $data = input("get.");
            $active=$model->where("switch", 1)->where('id', $data['id'])->find();
            $active['timer'] = config("visit_time2")[$active['timer']];
            $result['enday'] = ceil(round((strtotime($active['endday'])-strtotime($active['day']))/86400,1));
            for ($i=0;$i<=$result['enday'];$i++){ //预约7天内的时间
                if(strtotime($active['day']. "+".$i."day")<=time()){
                }else{
                    $time[]= date("Y-m-d",strtotime($active['day']. "+".$i."day"));
                }
            }
            $sum=$model1->where(["active_id"=>$data['id'],"status"=>1])->where("day",$time[0])->sum("views");
            if ($sum >= $active['views']) {
                $active['sum'] = 1;
            }else{
                $active['sum'] = $active['views']-$sum;
            }
            $model=new Area();
            $province =$model->where("pid",0)->column("id,name");   //获取省数据
            $this->assign("province",$province);
            $this->assign("active",$active);
            $this->assign("time",$time);
            return view();
        }
    }

    /**
     * 活动人数
     */
    public function persionnum(){
        $model=new Active();
        $model1=new Yactive();
        $data = array_filter(input("get."));
        $active = $model->where("switch", 1)->where('id', $data['id'])->find();
        $sum = $model1->where("active_id", $data['id'])->where("day",$data['day'])->sum("views");
        if ($sum >= $active['views']) {
            return $active['sum'] = 1;
        }else{
            return $active['sum'] = $active['views']-$sum;
        }
        return $active['sum'];
    }


    public function getDate()
    {
        $fDate = Book::where(["status"=>1,"type"=>0])->column("day");
        $cDate = Book::where(["status"=>1,"type"=>1])->column("day");
        $canNot=$can=[];
        foreach($fDate as $k=>$v){
            $date = explode('-',$v);
            $canNot[$k]= implode('-',[$date[0],$date[1]+0,$date[2]+0]);

        }
        foreach($cDate as $k=>$v){
            $date = explode('-',$v);
            $can[$k]= implode('-',[$date[0],$date[1]+0,$date[2]+0]);

        }

        $dates = ['canVIew'=>$can,'conNotView'=>$canNot];
        return json($dates);
    }




}